﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace remote_config_tools
{
    class dt_protocol
    {
        /// <summary>
        /// 产生mac地址
        /// </summary>
        /// <returns></returns>
        public static byte [] CreateNewMacAddress()
        {
            int min = 0;
            int max = 16;
            Random ro = new Random();
            byte[] retByte = new byte[6];
            for (int i = 0; i < retByte.Length; i++)
            {
                retByte[i]=(byte) ro.Next(min, max);
            }
            return retByte;
        }
        public static string CreateNewMacAddressStr()
        {
            int min = 0;
            int max = 16;
            Random ro = new Random();
            byte[] retByte = new byte[6];
            for (int i = 0; i < retByte.Length; i++)
            {
                retByte[i] = (byte)ro.Next(min, max);
            }
            string mac = null;
            for (int i = 0; i < retByte.Length; i++)
            {
               mac= mac+  String.Format("{0:X2}", retByte[i]);
            }
            return mac;
        }
        public static byte[] wrapper_error_data()
        {
            byte[] send_buff = new byte[17];
            //信息头
            send_buff[0] = 0xaa;
            //信息长度
            send_buff[1] = 0x11;
            //描述符
            send_buff[2] = 0x10;
            send_buff[3] = 0x00;
            //设备信息
            for (int i = 0; i < 8; i++) send_buff[i + 4] = 0x00;
            //消息类型
            send_buff[12] = 0x28;
            send_buff[13] = 0x15;
            //保留位
            send_buff[14] = 0x00;
            send_buff[15] = 0x00;
            //校验和
            send_buff[16] = crcCalculate(send_buff, 16);
            //返回二进制字符串
            return send_buff;
        }
        public static byte[] wrapper_para_data(int sensor_type, byte[] para)
        {
            byte[] send_buff = new byte[27];
            //信息头
            send_buff[0] = 0xaa;
            //信息长度
            send_buff[1] = 0x1b;
            //描述符
            send_buff[2] = 0x10;
            send_buff[3] = 0x00;
            //设备信息
            for (int i = 0; i < 8; i++) send_buff[i + 4] = 0x00;
            //消息类型
            send_buff[12] = 0x50;
            send_buff[13] = 0x01;
            //保留位
            send_buff[14] = (byte)(sensor_type >> 8);
            send_buff[15] = (byte)(sensor_type >> 0);
            //有效数据域
            for (int i = 0; i < 10; i++)
            {
                send_buff[16 + i] = para[i];
            }
            //校验和
            send_buff[26] = crcCalculate(send_buff, (ushort)send_buff.Length);
            //返回二进制字符串
            return send_buff;
        }
        public static byte[] wrapper_para_qurey(int sensor_type, byte[] para)
        {
            byte[] send_buff = new byte[19];
            //信息头
            send_buff[0] = 0xaa;
            //信息长度
            send_buff[1] = 0x13;
            //描述符
            send_buff[2] = 0x10;
            send_buff[3] = 0x00;
            //设备信息
            for (int i = 0; i < 8; i++) send_buff[i + 4] = 0x00;
            //消息类型
            send_buff[12] = 0x50;
            send_buff[13] = 0x01;
            //保留位
            send_buff[14] = (byte)(sensor_type >> 8);
            send_buff[15] = (byte)(sensor_type >> 0);
            //有效数据域
            for (int i = 0; i < 2; i++)
            {
                send_buff[16 + i] = para[i];
            }
            //校验和
            send_buff[18] = crcCalculate(send_buff, (ushort)send_buff.Length);
            //返回二进制字符串
            return send_buff;
        }
        public static byte[] wrapper_mac_data(byte[] mac)
        {
            byte[] send_buff = new byte[23];
            //信息头
            send_buff[0] = 0xaa;
            //信息长度
            send_buff[1] = 0x17;
            //描述符
            send_buff[2] = 0x10;
            send_buff[3] = 0x00;
            //设备信息
            for (int i = 0; i < 8; i++) send_buff[i + 4] = 0x00;
            //消息类型
            send_buff[12] = 0x21;
            send_buff[13] = 0x23;
            //保留位
            send_buff[14] = 0x00;
            send_buff[15] = 0x00;
            for (int i = 0; i < mac.Length; i++)
            {
                send_buff[16 + i] = mac[i];
            }
            //校验和
            send_buff[22] = crcCalculate(send_buff, (ushort)send_buff.Length);
            //返回二进制字符串
            return send_buff;
        }
        public static byte[] wrapper_upgrade_data(byte[] data,byte version,long sum_pack,long pack_num)
        {
            byte[] send_buff= new byte[88];
            //信息头
            send_buff[0] = 0xaa;
            //信息长度
            send_buff[1] =(byte)(0x18+64);
            //描述符
            send_buff[2] = 0x10;
            send_buff[3] = 0x00;
            //设备信息
            for (int i = 0; i < 8; i++) send_buff[i + 4] = 0x00;
            //消息类型
            send_buff[12] = 0x40;
            send_buff[13] = 0x01;
            //保留位
            send_buff[14] = 0x00;
            send_buff[15] = 0x00;
            //固件类型，00代表APP，01代表boot，目前只支持APP
            send_buff[16] = 0x00;
            //固件版本号
            send_buff[17] = version;
            //升级文件总包数
            send_buff[18] = (byte)(sum_pack>>8);
            send_buff[19] = (byte)(sum_pack >> 0);
            //当前包序号
            send_buff[20] = (byte)(pack_num >> 8);
            send_buff[21] = (byte)(pack_num >> 0);
            //包长度
            send_buff[22] = (byte)data.Length;
            //包数据
            Array.Copy(data, 0, send_buff, 23, data.Length);
            //校验和
            send_buff[87] = crcCalculate(send_buff, (ushort)send_buff.Length);
            //返回二进制字符串
            return send_buff;
        }
        /// <summary>
        /// 发送缓冲区
        /// </summary>
        //private static byte[] send_buff = new byte[255];
        /// <summary>
        /// 传感器数据封装
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="humi"></param>
        /// <param name="voc"></param>
        /// <param name="co2"></param>
        /// <param name="pm25"></param>
        /// <param name="hcho"></param>
        /// <param name="lux"></param>
        /// <returns></returns>
        public static byte [] wrapper_sensor_data(string temp, string humi, string voc, string co2, string pm25, string hcho, string lux)
        {
            byte[] send_buff = new byte[47];
            //信息头
            send_buff[0] = 0xaa;
            //信息长度
            send_buff[1] = 0x2f;
            //描述符
            send_buff[2] = 0x10;
            send_buff[3] = 0x00;
            //设备信息
            for (int i = 0; i < 8; i++) send_buff[i + 4] = 0x00;
            //消息类型
            send_buff[12] = 0x20;
            send_buff[13] = 0x05;
            //保留位
            send_buff[14] = 0x00;
            send_buff[15] = 0x00;
            //消息-----传感器数据
            send_buff[16] = (byte)Convert.ToInt16(temp);
            send_buff[17] = Convert.ToByte(humi);
            send_buff[18] = Convert.ToByte(voc);
            send_buff[19] = (byte)(Convert.ToUInt16(co2) >> 8);
            send_buff[20] = (byte)(Convert.ToUInt16(co2) >> 0);
            send_buff[21] = (byte)(Convert.ToUInt16(pm25) >> 8);
            send_buff[22] = (byte)(Convert.ToUInt16(pm25) >> 0);
            send_buff[23] = (byte)(Convert.ToUInt16(hcho) >> 8);
            send_buff[24] = (byte)(Convert.ToUInt16(hcho) >> 0);
            send_buff[25] = (byte)(Convert.ToUInt16(lux) >> 8);
            send_buff[26] = (byte)(Convert.ToUInt16(lux) >> 0);
            //消息-----智能策略
            //空调开关
            send_buff[27] = 00;
            //空调温度
            send_buff[28] = 24;
            //空调模式
            send_buff[29] = 01;
            //空调设备类型
            send_buff[30] = 00;
            send_buff[31] = 01;
            //甲醛开关
            send_buff[32] = 00;
            //甲醛设备类型
            send_buff[33] = 00;
            send_buff[34] = 01;
            //CO2开关
            send_buff[35] = 00;
            //CO2设备类型
            send_buff[36] = 00;
            send_buff[37] = 01;
            //PM25开关
            send_buff[38] = 00;
            //PM25设备类型
            send_buff[39] = 00;
            send_buff[40] = 01;
            //rtc 开关
            send_buff[41] = 00;
            //rtc repeat
            send_buff[42] = 00;
            //rtc hour
            send_buff[43] = 01;
            //rtc min
            send_buff[44] = 30;
            //bat
            send_buff[45] = 90;
            //校验和
            send_buff[46] = crcCalculate(send_buff, (ushort)send_buff.Length);
            //bytes转字符串
            //sensor_policy_ret = ByteToHexStr(send_buff, 47);
            //返回二进制字符串
            return send_buff;
        }
        /// <summary>
        /// 电陶校验和计算
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="msg_len"></param>
        /// <returns></returns>
        public static byte crcCalculate(byte[] msg, UInt16 msg_len)
        {
            byte tst = 0;
            for (int i = 0; i < msg_len-1; i++) tst += msg[i];
            tst = (byte)(~((UInt16)tst));
            tst += 1;
            return tst;
        }
        /// <summary>
        /// byte[]转为16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteToHexStr(byte[] bytes, UInt16 msg_len)
        {
            string returnStr = "";
            for (int i = 0; i < msg_len; i++)
            {
                returnStr += bytes[i].ToString("X2");
            }
            return returnStr;
        }
        /// <summary>
        /// 将16进制的字符串转为byte[]
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] StrToHex(string mStr) //返回处理后的十六进制字符串
        {
           return ASCIIEncoding.Default.GetBytes(mStr);
        } 
    }
}
